<template>
  <div class="app-container">
    <el-card>
      <el-form ref="searchForm" :model="searchForm" inline size="small">
        <el-form-item label="素材名称:">
          <el-input
            clearable
            v-model="searchForm.search"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            size="small"
            icon="el-icon-refresh"
            @click="resetForm('searchForm')"
            >重置</el-button
          >
          <el-button
            type="primary"
            size="small"
            icon="el-icon-search"
            @click="getList()"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div>
        <el-button
          size="small"
          type="primary"
          @click="addArtice()"
          icon="el-icon-plus"
          >添加素材</el-button
        >
      </div>

      <el-table
        :data="tableData.data"
        v-loading="listLoading"
        border
        style="width: 100%; margin-top: 20px"
        size="small"
      >
        <el-table-column align="center" type="index" label="编号" width="80" />
        <el-table-column
          align="center"
          prop="title"
          label="标题名称"
           :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column align="center" prop="pic" label="封面图">
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.pic"
              :preview-src-list="[scope.row.pic]"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="type" label="类别">
          <template slot-scope="scope">
            <el-tag size="mini" type="success" v-if="scope.row.type == 3"
              >视频</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="note"
          label="备注"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column align="center" prop="createTime" label="创建时间">
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button size="mini" type="text" @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        class="pageRight"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 30, 40]"
        :current-page="searchForm.pageNum"
        :page-size="searchForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getThirdMaterialListPage, deleteThirdMaterial } from "@/api/lesson";
export default {
  name: "Article",
  components: {
    // Tinymce
  },
  data() {
    return {
      props: {
        emitPath: false
      },
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        search: "",
        beginTime: "",
        endTime: "",
        categoryId: ""
      },
      tableData: {
        data: [],
        total: 0
      },
      listLoading: false,
      deleteList: [],
      cateList: []
    };
  },
  async created() {
    await this.getList();
  },
  mounted() {},
  methods: {
    handleClick(item) {
      console.log(item.menu);

      if (item.menu == 1) {
        this.$router.push({
          path: "/lessonMaterialadd/addlist"
          // query: {
          //   id: v.id
          // }
        });
      }
    },

    resetForm(form) {
      // this.$refs[form].resetFields();
      this.searchForm = {};
      this.getList();
    },
    selectTableList(val) {
      this.deleteList = val.map(v => v.id);
    },

    deleteMany() {
      let that = this;
      if (this.deleteList.length == 0) {
        this.$message({
          message: "请先选择要删除的项",
          type: "warning"
        });
      } else {
        this.$confirm("是否确认删除?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          deleteArticleBatch(this.deleteList.join(",")).then(res => {
            if (res.code == 200) {
              that.$message({
                type: "success",
                message: "删除成功!"
              });
              setTimeout(() => {
                that.getList();
              }, 500);
            }
          });
        });
      }
    },

    handleEdit(v) {
      this.$router.push({
        path: "/lessonMaterialadd/addlist",
        query: {
          id: v.id
        }
      });
    },
    handleDelete(v) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteThirdMaterial(v.id).then(res => {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.getList();
        });
      });
    },
    handleDetail(v) {
      this.$router.push({
        path: "/article/detail",
        query: {
          id: v.id
        }
      });
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.searchForm.pageNum = val;
      this.getList();
    },
    addArtice() {
      this.$router.push({
        path: "/lessonMaterialadd/addlist"
        // query: {
        //   id: v.id
        // }
      });
    },
    getList() {
      this.listLoading = true;
      getThirdMaterialListPage(this.searchForm)
        .then(res => {
          this.tableData.data = res.data.list;
          this.tableData.total = res.data.total;
          this.listLoading = false;
        })
        .finally(() => {
          this.listLoading = false;
        });
    }
  }
};
</script>
<style lang="scss" scoped>
.app-container {
  height: calc(100vh - 50px);
  background-color: #f5f5f5;
  position: relative;
}
</style>
